/*
 * @Description: 
 * @Author: Aroma
 * @Date: 2024-11-25 15:13:10
 * @LastEditors: Aroma
 * @LastEditTime: 2024-11-25 15:26:44
 * @别瞎看: 你个无脑牛马
 */
package com.briup.demo.mapper;
import com.briup.demo.User;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface UserMapper {
    //写SQL语句
    //@Select代表写的是select语句
    @Select("select * from t_user")
    public List<User> getAllUser();

    //根据id删除用户
    @Delete("delete from t_user where id = #{id}")
    public void deleteById(int id);

    //起别名解决 映射失败问题
    @Select("select id,name,phone,gender,birthday,register_time as registerTime from t_user where id=#{id}")
    public User getById(Integer id);

    //组合查询
    //查询姓张的同学，性别女   #{'张%'}   '张%'and gender = 2
    //注册时间在2019年1月1日之后的      register_time > '2019-01-01'
    //结果根据注册时间降序    order by register_time desc
//    @Select("select * from t_user  " +
//            "where name like  '%${name}%' " +
//            "and gender = ${gender = 2} " +
//            "and register_time between #{startTime} and #{endTime} " +
//            "order by register_time desc")
//    public List<User> getUserByNameAndGender
//    (@Param("name") String name,@Param("gender") short gender,@Param("startTime") LocalDateTime startTime,@Param("endTime") LocalDateTime endTime);

    @Select("SELECT * FROM t_user " +
            "WHERE name LIKE CONCAT('%', #{name},'%')" +
            "AND gender = #{gender} " +
            "AND register_time BETWEEN #{startTime} AND #{endTime} " +
            "ORDER BY register_time DESC")
    public List<User> getUserByNameAndGender
    (@Param("name") String name,@Param("gender") short gender,@Param("startTime") LocalDateTime startTime,@Param("endTime") LocalDateTime endTime);

    //登录
    //无法防止sql注入的
    //@Select("select * from t_user where name = #{name} and password = #{password}")
    //防止sql注入的
    @Select("select * from t_user where id = ${id} and phone=${phone}")
    public List<User> login(@Param("id") int id,@Param("phone") String phone);

    //新增 - 注册
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into t_user(name,age,gender,phone) values (#{name},#{age},#{gender},#{phone})")
    public void addUser(User user);

    //编辑
    @Update("update t_user set name = #{name},age=#{age},gender=#{gender},phone=#{phone} where id = #{id}")
    public void updateUser(User user);


}
